package com.example.forestmanagement;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ProfileActivity extends AppCompatActivity {
    private static final String BASE_URL = "http://8.130.186.54:9898";
    private OkHttpClient client = new OkHttpClient();
    
    private TextView tvUserName;
    private TextView tvUserJob;
    private TextView tvUserGroup;
    private TextView tvUserAccount;
    private TextView tvUserSex;
    private TextView tvUserAge;
    private TextView tvUserSelfAccount;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        
        // 初始化视图
        initViews();
        
        // 加载用户信息
        loadUserProfile();
    }
    
    private void initViews() {
        tvUserName = findViewById(R.id.tv_user_name);
        tvUserJob = findViewById(R.id.tv_user_job);
        tvUserGroup = findViewById(R.id.tv_user_group);
        tvUserAccount = findViewById(R.id.tv_user_account);
        tvUserSex = findViewById(R.id.tv_user_sex);
        tvUserAge = findViewById(R.id.tv_user_age);
        tvUserSelfAccount = findViewById(R.id.tv_user_self_account);
        
        // 设置返回按钮
        findViewById(R.id.btn_back).setOnClickListener(v -> finish());
    }
    
    private void loadUserProfile() {
        SharedPreferences sp = getSharedPreferences("user_info", MODE_PRIVATE);
        String token = sp.getString("token", "");
        int userId = sp.getInt("userId", 0);
        
        Request request = new Request.Builder()
                .url(BASE_URL + "/app-api/linban/user/get?id=" + userId)
                .addHeader("Authorization", "Bearer " + token)
                .get()
                .build();
                
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(() -> Toast.makeText(ProfileActivity.this, 
                        "加载失败: " + e.getMessage(), Toast.LENGTH_SHORT).show());
            }
            
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    String responseData = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseData);
                    if (jsonObject.getInt("code") == 0) {
                        JSONObject data = jsonObject.getJSONObject("data");
                        runOnUiThread(() -> updateUI(data));
                    } else {
                        runOnUiThread(() -> Toast.makeText(ProfileActivity.this,
                                "加载失败: " + jsonObject.optString("msg"), Toast.LENGTH_SHORT).show());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    runOnUiThread(() -> Toast.makeText(ProfileActivity.this,
                            "数据解析失败", Toast.LENGTH_SHORT).show());
                }
            }
        });
    }
    
    private void updateUI(JSONObject data) {
        try {
            tvUserName.setText("姓名：" + data.optString("userName"));
            tvUserJob.setText("职位：" + data.optString("job"));
            tvUserGroup.setText("工作组：" + data.optString("jobGroup"));
            tvUserAccount.setText("账号：" + data.optString("account"));
            tvUserSex.setText("性别：" + data.optString("sex"));
            tvUserAge.setText("年龄：" + data.optInt("age"));
            tvUserSelfAccount.setText("自定义账号：" + data.optString("selfAccount"));
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "更新界面失败", Toast.LENGTH_SHORT).show();
        }
    }
} 